package pl.msikora.android.lapmagic.database;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SplitsTable {
	
	private static final String TAG = "SplitsTable";
	
	public static final String TABLE_SPLITS = "splits";
	public static final String COLUMN_LAP_SESSION_ID = "lap_session_id";
	public static final String COLUMN_LAP_NO = "lap_no";
	public static final String COLUMN_SPLIT_NAME = "split_name";
	public static final String COLUMN_ON_SPLIT_TIME = "on_split_time";
	
	private static final String TABLE_CREATE = "create table "
			+ TABLE_SPLITS
			+ "("
			+ COLUMN_LAP_SESSION_ID + " integer not null, " // references laps on delete cascade
			+ COLUMN_LAP_NO + " integer not null, " // references laps on delete cascade
			+ COLUMN_SPLIT_NAME + " text not null, "
			+ COLUMN_ON_SPLIT_TIME + " integer not null, " // in milliseconds 
			+ "primary key(" + COLUMN_LAP_SESSION_ID + ", " + COLUMN_LAP_NO + ", " 
			+ COLUMN_SPLIT_NAME + ")"
			+ ");";
	
	public static void onCreate(SQLiteDatabase database) {
		Log.d(TAG, "creating table splits with statement '" + TABLE_CREATE
				+ "'");
		database.execSQL(TABLE_CREATE);
	}
	
	public static void onUpgrade(SQLiteDatabase database, int oldVersion, 
			int newVersion) {
		Log.w(TAG, "Upgrading database from version " + oldVersion
				+ " to " + newVersion + ", which will destroy new all old data");
		database.execSQL("drop table if exists " + TABLE_SPLITS);
		onCreate(database);
	}

}
